package com.ylkj.patientdrug.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.ylkj.patientdrug.entity.DrugPackEntity;
import com.ylkj.patientdrug.R;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class SpecAdapter extends RecyclerView.Adapter<SpecAdapter.ViewHolder> {

    private Context mActivity;
    private List<DrugPackEntity> labelList;
    private SpecListener listener;

    public SpecAdapter(Context mActivity, List<DrugPackEntity> list) {
        this.labelList = list;
        this.mActivity = mActivity;
    }

    @NonNull
    @Override
    public SpecAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_spec_info, viewGroup, false);
        SpecAdapter.ViewHolder vh = new SpecAdapter.ViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(SpecAdapter.ViewHolder viewHolder, @SuppressLint("RecyclerView") int i) {
        viewHolder.item_rv_name.setText(labelList.get(i).getName());
        viewHolder.item_rv_spec.setText("规格：" + labelList.get(i).getSpec() + "x1");
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null){
                    listener.onClick(i);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return labelList != null ? labelList.size() : 0;
    }

    public void setListener(SpecListener listener) {
        this.listener = listener;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView item_rv_name;
        private TextView item_rv_spec;

        public ViewHolder(View view) {
            super(view);
            item_rv_name = view.findViewById(R.id.item_rv_name);
            item_rv_spec = view.findViewById(R.id.item_rv_spec);

        }
    }

    public List<DrugPackEntity> getLabelList() {
        return labelList;
    }

    public void setLabelList(List<DrugPackEntity> labelList) {
        this.labelList = labelList;
        notifyDataSetChanged();
    }

    public interface SpecListener{
        void onClick(int index);
    }
}
